﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FreedomERP.Models;
using System.Collections;
using System.Data.Objects;
using System.Text;
using FreedomERP.Comm.Utils;

namespace FreedomERP.Dao.Inv.Impl
{
    public class InvReceiveOrderDetailDAOImpl:IInvReceiveOrderDetailDAO
    {
        FreedomERPEntities dbEntities = new FreedomERPEntities();

        /// <summary>
        /// 根据FParentId获取明细列表
        /// 作者：梁建东
        /// </summary>
        public List<InvReceiveOrderDetail> GetReceiveOrderDetailListByFParentId(string FParentId)
        {
            List<InvReceiveOrderDetail> list = (from irod in dbEntities.InvReceiveOrderDetail where irod.FParentId == FParentId select irod).ToList<InvReceiveOrderDetail>();
            return list;
        }

        /// <summary>
        /// 根据条件获取明细列表
        /// 作者：梁建东
        /// </summary>
        public List<InvReceiveOrderDetail> GetReceiveOrderDetailListByFilter(Hashtable filters)
        {
            //where
            List<ObjectParameter> paramList = new List<ObjectParameter>();
            StringBuilder sbWhere = new StringBuilder();
            if (filters["FProductNumber"] != null && !filters["FProductNumber"].ToString().Equals(""))
            {
                sbWhere.Append("it.BdProduct.FNumber like @FProductNumber and ");
                ObjectParameter param = new ObjectParameter("FProductNumber", "%" + filters["FProductNumber"].ToString() + "%");
                paramList.Add(param);
            }
            if (filters["FProductName"] != null && !filters["FProductName"].ToString().Equals(""))
            {
                sbWhere.Append("it.BdProduct.FName like @FProductName and ");
                ObjectParameter param = new ObjectParameter("FProductName", "%" + filters["FProductName"].ToString() + "%");
                paramList.Add(param);
            }
            if (filters["FDateFrom"] != null && !filters["FDateFrom"].ToString().Equals(""))
            {
                sbWhere.Append("it.InvReceiveOrder.FBillDate between @FDateFrom and @FDateTo and ");
                ObjectParameter param = new ObjectParameter("FDateFrom", DateUtils.ParseStringToDateTime(filters["FDateFrom"].ToString()));
                paramList.Add(param);
                string FBillDateTo = "";
                if (filters["FDateTo"] != null && !filters["FDateTo"].ToString().Equals(""))
                {
                    FBillDateTo = filters["FDateTo"].ToString();
                }
                else
                {
                    FBillDateTo = DateUtils.GetCurDateFormat("yyyy/MM/dd HH:mm:ss");
                }
                ObjectParameter paramT = new ObjectParameter("FDateTo", DateUtils.ParseStringToDateTime(FBillDateTo));
                paramList.Add(paramT);
            }
            if (filters["FStatusId"] != null && !filters["FStatusId"].ToString().Equals(""))
            {
                sbWhere.Append("it.InvReceiveOrder.FStatusId=@FStatusId and ");
                ObjectParameter param = new ObjectParameter("FStatusId", filters["FStatusId"].ToString());
                paramList.Add(param);
            }

            sbWhere.Append("it.InvReceiveOrder.FOrgunitId=@orgunitParam and ");
            ObjectParameter paramOrg = new ObjectParameter("orgunitParam", filters["orgunitParam"].ToString());
            paramList.Add(paramOrg);

            sbWhere.Append("1=1");

            ObjectParameter[] objParameters = new ObjectParameter[paramList.Count];
            int c = 0;
            foreach (ObjectParameter paramTemp in paramList)
            {
                objParameters[c] = paramTemp;
                c++;
            }

            var query = dbEntities.InvReceiveOrderDetail.Where(sbWhere.ToString(), objParameters);

            List<InvReceiveOrderDetail> list = query.ToList();

            return list;
        }
    }
}